package com.gwcm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gwcm.exception.BusinessException;
import com.gwcm.model.dto.category.CategoryAddRequest;
import com.gwcm.model.dto.category.CategoryDeleteRequest;
import com.gwcm.model.dto.category.CategoryQueryRequest;
import com.gwcm.model.dto.category.CategoryUpdateRequest;
import com.gwcm.model.entity.Category;
import com.gwcm.model.vo.CategoryVo;
import com.gwcm.service.CategoryService;
import com.yhz.commonutil.common.BaseResponse;
import com.yhz.commonutil.common.ErrorCode;
import com.yhz.commonutil.common.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/category")
@Api(tags = "商品类别接口")
public class CategoryController {
    @Autowired
    public CategoryService categoryService;

    @ApiOperation("新增类别")
    @PreAuthorize("hasAuthority('pos:category:add')")
    @PostMapping("/save")
    public BaseResponse addCategory(@RequestBody CategoryAddRequest categoryAddRequest) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryAddRequest, category);

        categoryService.saveCategory(category);

        return ResultUtils.success();
    }

    @ApiOperation("分页查询类别接口")
    @PreAuthorize("hasAuthority('pos:category:list')")
    @PostMapping("/page")
    public BaseResponse<IPage<CategoryVo>> pageModuleTopic(@RequestBody CategoryQueryRequest categoryQueryRequest) throws BusinessException {
        IPage<CategoryVo> categoryVoIPage = categoryService.pageList(categoryQueryRequest);
        return ResultUtils.success(categoryVoIPage);
    }

    @ApiOperation("修改类别")
    @PreAuthorize("hasAuthority('pos:product:edit')")
    @PostMapping("/update")
    public BaseResponse updateCategory(@RequestBody CategoryUpdateRequest categoryUpdateRequest) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryUpdateRequest, category);

        categoryService.updateCategory(category);

        return ResultUtils.success();
    }

    @ApiOperation("删除类别")
    @PreAuthorize("hasAuthority('pos:product:delete')")
    @PostMapping("/delete")
    public BaseResponse deleteCategory(@RequestBody CategoryDeleteRequest categoryDeleteRequest) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDeleteRequest, category);

        boolean delete = categoryService.removeById(category);
        if (!delete) {
            throw new BusinessException(ErrorCode.DELETE_ERROR);
        }
        return ResultUtils.success();
    }
}
